package com.sc.nft.entity.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sc.nft.dto.SaasTaskMeetConditionsAddDTO;
import com.sc.nft.dto.SaasTaskMeetConditionsOptionAddDTO;
import com.sc.nft.entity.SaasTask;
import com.sc.nft.entity.SaasTaskMeetConditions;
import com.sc.nft.entity.SaasTaskMeetConditionsLocation;
import com.sc.nft.entity.SaasTaskMeetConditionsOption;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class SaasTaskModificationStarDTO {
    @ApiModelProperty("任务id")
    @NotNull(message = "任务id不能为空")
    private Long id;

    @ApiModelProperty(value = "任务名字")
    private String taskName;

    @ApiModelProperty(value = "任务icon")
    private String taskIcon;

    @ApiModelProperty(value = "任务完成人数")
    private Integer taskSum;

    @ApiModelProperty(value = "任务重置类型")
    private SaasTaskRestEnum taskRest;

    @ApiModelProperty("在持有人为指定藏品的时候.需要对应的藏品id")
    private List<Long> taskParticipatorCollectionIds;

    @ApiModelProperty(value = "是否是首次参与,目前只用到了熔炼,意思就是是不是从注册开始第一次熔炼")
    private Boolean firstParticipation;

    @ApiModelProperty(value = "任务参与者 1:无门槛  2:持有指定藏品")
    private SaasTaskParticipatorEnum taskParticipator;

    @ApiModelProperty(value = "可见对象  1:所有人 2:DAO主理人网体")
    private SaasTaskVisibleEnum taskVisible;

    @ApiModelProperty(value = "任务完成总次数")
    @Range(min = 1, max = 999999, message = "任务每个人最大完成上限区间范围不正确")
    private Integer taskTotalSum;

    @ApiModelProperty(value = "任务每个人最大完成上限")
    private Integer taskEveryoneSum;

    @ApiModelProperty(value = "开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskStart;

    @ApiModelProperty(value = "结束时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskEnd;

    @ApiModelProperty(value = "任务条件")
    private SaasTaskMeetConditionsAddDTO saasTaskMeetConditionsAddDTO;

    @ApiModelProperty(value = "任务条件详情")
    private List<@Valid SaasTaskMeetConditionsOptionAddDTO> saasTaskMeetConditionsOptionAddDTO;

    @JsonIgnore
    private SaasTaskMeetConditions saasTaskMeetConditions;

    @JsonIgnore
    private List<SaasTaskMeetConditionsOption> saasTaskMeetConditionsOptions;

    @ApiModelProperty("任务的生效位置")
    private List<SaasTaskAddMeetConditionsLocationDTO> saasTaskAddMeetConditionsLocation;

    @ApiModelProperty("任务的生效位置")
    @JsonIgnore
    private List<SaasTaskMeetConditionsLocation> saasTaskMeetConditionsLocations = new ArrayList<>();


    public Boolean validator(SaasTask saasTask) {
        if (BeanUtil.isEmpty(saasTask)) {
            throw new GlobalRunTimeException("任务不存在");
        }
        // 如果当前任务审核已经通过,则不能进行任何修改,只能上下架
        if (saasTask.getTaskConfiguration() == SaasTaskConfigurationEnum.APPROVED) {
            throw new GlobalRunTimeException("任务已经审核通过,不能进行修改");
        }
        if (saasTask.getTaskConfiguration() == SaasTaskConfigurationEnum.UNDER_REVIEW) {
            throw new GlobalRunTimeException("任务审核中,需要修改请先撤销申请");
        }
        return true;
    }


    /**
     * 处理任务条件
     *
     * @param id 任务基础类型id
     * @return
     */
    public void processingTaskCondition(Long id) {
        SaasTaskTypeEnum taskType = this.saasTaskMeetConditionsAddDTO.getTaskType();
        // 根据类型验证参数是否都对
        verificationSaasTaskMeetConditions(taskType);
        // 根据类型,把参数转换为实体类
        SaasTaskMeetConditions saasTaskMeetConditions = BeanUtil.copyProperties(this.saasTaskMeetConditionsAddDTO, SaasTaskMeetConditions.class);
        saasTaskMeetConditions.setSaasTaskId(id);
        this.saasTaskMeetConditions = saasTaskMeetConditions;
    }


    /**
     * 把任务条件详情转换为实体类
     *
     * @param taskId
     * @param meetConditionsId
     */
    public void processingTaskConditionOption(Long taskId, Long meetConditionsId) {
        List<SaasTaskMeetConditionsOption> saasTaskMeetConditionsOptions = BeanUtil.copyToList(this.saasTaskMeetConditionsOptionAddDTO, SaasTaskMeetConditionsOption.class);
        List<SaasTaskMeetConditionsOption> addConditions = new ArrayList<>();
        // 任务创建的时候,如果是需要持有指定藏品的情况下,把藏品id创建进去集合
        if (this.taskParticipator == SaasTaskParticipatorEnum.HOLD_COLLECTION) {
            if (CollUtil.isEmpty(this.taskParticipatorCollectionIds)) {
                throw new GlobalRunTimeException("任务参与者为持有指定藏品的时候,藏品id不能为空");
            }


            for (Long taskParticipatorCollectionId : this.taskParticipatorCollectionIds) {
                if (taskParticipatorCollectionId != null) {
                    SaasTaskMeetConditionsOption saasTaskMeetConditionsOption = new SaasTaskMeetConditionsOption();
                    saasTaskMeetConditionsOption.setSaasTaskId(taskId);
                    saasTaskMeetConditionsOption.setConditionId(taskParticipatorCollectionId);
                    saasTaskMeetConditionsOption.setTaskType(this.saasTaskMeetConditionsAddDTO.getTaskType());
                    saasTaskMeetConditionsOption.setType(1);
                    addConditions.add(saasTaskMeetConditionsOption);
                }
            }
        }

        // 判定当前的任务类型是否需要把任务条件详情转换为实体类
        if (this.saasTaskMeetConditionsAddDTO.getTaskType().getConfiguration()) {
            if (CollUtil.isNotEmpty(saasTaskMeetConditionsOptions)) {
                saasTaskMeetConditionsOptions.forEach(saasTaskMeetConditionsOption -> {
                    saasTaskMeetConditionsOption.setSaasTaskId(taskId);
                    saasTaskMeetConditionsOption.setMeetConditionsId(meetConditionsId);
                    SaasTaskTypeEnum taskType = this.saasTaskMeetConditionsAddDTO.getTaskType();
                    int type = 2;
                    if (taskType == SaasTaskTypeEnum.PURCHASE_SPECIFIED_IP_ITEMS) {
                        type = 3;
                    }
                    saasTaskMeetConditionsOption.setType(type);
                    saasTaskMeetConditionsOption.setTaskType(this.saasTaskMeetConditionsAddDTO.getTaskType());
                });
            } else {
                throw new GlobalRunTimeException("任务指定内容不能为空");
            }
        }
        saasTaskMeetConditionsOptions.addAll(addConditions);
        this.saasTaskMeetConditionsOptions = saasTaskMeetConditionsOptions.stream().filter(saasTaskMeetConditionsOption -> saasTaskMeetConditionsOption.getConditionId() != null).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(this.saasTaskAddMeetConditionsLocation)) {
            // 任务生效位置
            List<SaasTaskMeetConditionsLocation> saasTaskMeetConditionsLocations = BeanUtil.copyToList(this.saasTaskAddMeetConditionsLocation, SaasTaskMeetConditionsLocation.class);
            saasTaskMeetConditionsLocations.forEach(saasTaskMeetConditionsLocation -> {
                saasTaskMeetConditionsLocation.setMeetConditionsId(meetConditionsId);
                saasTaskMeetConditionsLocation.setSaasTaskId(taskId);
            });
            this.saasTaskMeetConditionsLocations = saasTaskMeetConditionsLocations;
        }
    }

    /**
     * 验证类型.对应类型请求参数不一样,不同的需要不同的必传参数
     *
     * @param saasTaskTypeEnum
     * @return
     */
    public Boolean verificationSaasTaskMeetConditions(SaasTaskTypeEnum saasTaskTypeEnum) {
        if (saasTaskTypeEnum == null) {
            throw new GlobalRunTimeException("任务类型不能为空");
        }
        // 首次开通钱包
        if (saasTaskTypeEnum == SaasTaskTypeEnum.OPEN_WALLET_FOR_THE_FIRST_TIME) {
            return true;
        }
        // 邀请首次开通钱包
        if (saasTaskTypeEnum == SaasTaskTypeEnum.FIRST_TIME_OPEN_WALLET_INVITATION) {
            if (this.saasTaskMeetConditionsAddDTO.getVoluntarySum() > 0) {
                return true;
            }
            throw new GlobalRunTimeException("邀请人数不能小于0");
        }
        // 购买指定藏品
        if (saasTaskTypeEnum == SaasTaskTypeEnum.PURCHASE_OF_DESIGNATED_COLLECTION) {
            if (CollUtil.isNotEmpty(this.saasTaskMeetConditionsOptionAddDTO) && NumberUtil.isGreater(this.saasTaskMeetConditionsAddDTO.getSchedule(), BigDecimal.ZERO)) {
                return true;
            }
            throw new GlobalRunTimeException("购买指定藏品列表不能为空或进度不能为0");
        }

        // 邀请购买指定藏品
        if (saasTaskTypeEnum == SaasTaskTypeEnum.INVITATION_TO_PURCHASE_SPECIFIED_COLLECTIONS) {
            if (CollUtil.isNotEmpty(this.saasTaskMeetConditionsOptionAddDTO)) {
                if (this.saasTaskMeetConditionsAddDTO.getVoluntarySum() > 0) {
                    return true;
                }
            }
            throw new GlobalRunTimeException("邀请人数不能小于0或进度不能为0");
        }
        if (saasTaskTypeEnum==SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_APPOINT_COLLECTION) {
            if (this.getFirstParticipation()) {
                if (CollUtil.isNotEmpty(this.saasTaskMeetConditionsOptionAddDTO)) {
                    if (this.saasTaskMeetConditionsAddDTO.getVoluntarySum() > 0) {
                        return true;
                    }
                }
                throw new GlobalRunTimeException("邀请人数不能小于0或进度不能为0");
            }
            return true;
        }

        // 购买指定ip商城商品
        if (saasTaskTypeEnum == SaasTaskTypeEnum.PURCHASE_SPECIFIED_IP_ITEMS) {
            if (CollUtil.isNotEmpty(this.saasTaskMeetConditionsOptionAddDTO) && NumberUtil.isGreater(this.saasTaskMeetConditionsAddDTO.getSchedule(), BigDecimal.ZERO)) {
                return true;
            }
            throw new GlobalRunTimeException("任务条件详情不能为空或进度不能为0");
        }

        if (saasTaskTypeEnum == SaasTaskTypeEnum.NEW_USERS_MAKE_THEIR_FIRST_PURCHASE) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.INVITE_NEW_USERS_TO_MAKE_THEIR_FIRST_PURCHASE) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.ACCUMULATED_CONSUMPTION_AMOUNT) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.BUY_BRAND_HATCHING_ANY_COLLECTION) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.PURCHASE_PLATFORM_DEBUT_ANY_COLLECTION) {
            return true;
        }

        if (saasTaskTypeEnum == SaasTaskTypeEnum.SMELTING_APPOINT_COLLECTION) {
            return true;
        }

        if (saasTaskTypeEnum == SaasTaskTypeEnum.SMELTING_WILL_COLLECTION) {
            return true;
        }

        if (saasTaskTypeEnum == SaasTaskTypeEnum.PASSIVITY_INVITE_SMELTING_WILL_COLLECTION) {
            return true;
        }


        if (saasTaskTypeEnum == SaasTaskTypeEnum.SMELTING_WILL_COLLECTION) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.CDK_REDEMPTION_TASK) {
            this.saasTaskMeetConditionsAddDTO.setSchedule(BigDecimal.ONE);
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.COMPOSITE_COLLECTION) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.DISASSEMBLE_THE_COLLECTION) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.SMELTING_COLLECTION) {
            return true;
        }
        if (saasTaskTypeEnum == SaasTaskTypeEnum.FREE_MARKET_PURCHASE_OF_ANY_COLLECTION) {
            return true;
        }
        throw new GlobalRunTimeException("请求参数异常");
    }
}
